package com.gljx.web.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.gljx.constant.ActivitiKey;
import com.gljx.util.ActitiviUtil;
import com.gljx.util.Axamine;
import com.gljx.util.ServerResponse;
import org.activiti.rest.common.api.ActivitiUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gljx.util.GetFid;
import com.gljx.web.Entity.Car;
import com.gljx.web.Entity.CarMaintainReg;
import com.gljx.web.Entity.CarMaintainRegEntry;
import com.gljx.web.Entity.User;
import com.gljx.web.dao.CarMaintainRegEntryMapper;
import com.gljx.web.dao.CarMaintainRegMapper;
import com.gljx.web.dao.CarMapper;
import com.gljx.web.dao.CommonDao;
import com.gljx.web.service.CarMaintainRegService;

@Service
@Transactional
public class CarMaintainRegServiceImpl extends CommonDao implements CarMaintainRegService{

	@Autowired
	private CarMaintainRegEntryMapper carMaintainRegEntryMapper;
	
	@Autowired
	private CarMaintainRegMapper carMaintainRegMapper;
	
	@Autowired
	private CarMapper carMapper;
	
	
	/**
	 *取分页数据    dmz 20160921
	 *@param pindex 页码
	 *@param psql SQL语句，需要带 rownum rn 例如：select rownum rn,t.* from zzda t
	 *@param psize 每页记录数
	 * */
	@Override
	public Map<String, Object> selectPage(int pindex, String psql, int psize) {
		  Map<String, Object> paramMap = new HashMap<String, Object>();
	       paramMap.put("Pindex", pindex);
	       paramMap.put("Psql", psql);
	       paramMap.put("Psize", psize);
	       paramMap.put("Pcount", oracle.jdbc.OracleTypes.INTEGER);
	       paramMap.put("v_cur", oracle.jdbc.OracleTypes.CURSOR);
	       this.getSqlSession().select("com.gljx.web.dao.CarMaintainRegMapper.pagemap", paramMap, null);
	       return  paramMap;
	}

	/**
	 * 取记录总数      dmz 20160921
	 * @param psql   SQL语句
	 * @return
	 */
	@Override
	public Map<String, Object> selectPageCount(String psql) {
		   Map<String, Object> paramMap = new HashMap<String, Object>();
	       paramMap.put("Psql", psql);
	       paramMap.put("Pcount", oracle.jdbc.OracleTypes.INTEGER);
	       this.getSqlSession().select("com.gljx.web.dao.CarMaintainRegMapper.pagecount", paramMap, null);
	       return paramMap;
	}

	/**
	 * 根据条件查询子类信息
	 */
	@Override
	public List<CarMaintainRegEntry> queryCarMaintainRegEntry(String fparentId, String carName, String carNumber) {
		// TODO Auto-generated method stub
		   Map<String, Object> paramMap = new HashMap<String, Object>();
	       paramMap.put("v_fparentid", fparentId);
	       paramMap.put("v_carName", carName);
	       paramMap.put("v_carNumber", carNumber);
	       paramMap.put("v_cursor", oracle.jdbc.OracleTypes.CURSOR);
	       this.getSqlSession().select("com.gljx.web.dao.CarMaintainRegEntryMapper.selectCarMaintainRegEntry", paramMap, null);
	       @SuppressWarnings("unchecked")
		   List<CarMaintainRegEntry> carMaintainRegEntrys = (List<CarMaintainRegEntry>) paramMap.get("v_cursor");
		   return carMaintainRegEntrys;
	}

	/**
	 * 添加或修改
	 */
	@Override
	public void editCarMaintainReg(CarMaintainReg carMaintainReg, String[] fseq, String[] cfcarnumer, String[] cfcarid,
			String[] cfbizdate, String[] cfenddate, String[] cfamount, String[] cfremark, User user, String time) {
		// TODO Auto-generated method stub
		if("".equals(carMaintainReg.getFid()) || carMaintainReg.getFid() == null){
			//创建人id
			carMaintainReg.setFcreatorid(user.getFid());
			//利用UUID生成fid
			carMaintainReg.setFid(GetFid.UUIDIntoFid());
			//当前时间
			carMaintainReg.setFcreatetime(time);
			//状态
            carMaintainReg.setCfbizstate(3L);
            //添加
			carMaintainRegMapper.insertSelective(carMaintainReg);
		}else{
			//修改时间时间
			carMaintainReg.setFlastupdatetime(time);
			//修改人id
			carMaintainReg.setFlastupdateuserid(user.getFid());
			//修改
			carMaintainRegMapper.updateByPrimaryKeySelective(carMaintainReg);
			//先删除子类信息
			carMaintainRegEntryMapper.deleteByFparentId(carMaintainReg.getFid());
		}
		//添加子类
		List<CarMaintainRegEntry> carMaintainRegEntrys = new ArrayList<>();
		if(fseq == null || fseq.length == 0){
			
		}else{
			for (int i = 0; i < fseq.length; i++) {
				CarMaintainRegEntry carMaintainRegEntry = new CarMaintainRegEntry();
				carMaintainRegEntry.setCfcarnumer(cfcarnumer[i]);
				carMaintainRegEntry.setCfcarid(cfcarid[i]);
				carMaintainRegEntry.setCfbizdate(cfbizdate[i]);
				carMaintainRegEntry.setCfenddate(cfenddate[i]);
				if("".equals(cfamount[i])){
					
				}else{
					carMaintainRegEntry.setCfamount(new BigDecimal(cfamount[i]));
				}
				carMaintainRegEntry.setCfremark(cfremark[i]);
				carMaintainRegEntry.setFseq((long)i+1);
				carMaintainRegEntry.setFid(GetFid.UUIDIntoFid());
				carMaintainRegEntry.setFparentid(carMaintainReg.getFid());
				carMaintainRegEntrys.add(carMaintainRegEntry);
			}
			carMaintainRegEntryMapper.insertCarMaintainRegEntry(carMaintainRegEntrys);
		}
	}

	/**
	 * 根据fid查询
	 */
	@Override
	public CarMaintainReg queryCarMaintainRegByFid(String fid) {
		// TODO Auto-generated method stub
		return carMaintainRegMapper.selectCarMaintainRegByFid(fid);
	}

	/**
	 * 根据fid删除
	 */
	@Override
	public void deleteCarMaintainRegByFid(String fid, String[] deleteId, String[] fnumber, String fnumber2) {
		// TODO Auto-generated method stub
		ActitiviUtil activitiUtil = new ActitiviUtil();
		if(fid == null || "".equals(fid)){
			for (int i = 0; i < deleteId.length; i++) {
				//先删除子类信息
				carMaintainRegEntryMapper.deleteByFparentId(deleteId[i]);
				//在删除父类信息
				carMaintainRegMapper.deleteByPrimaryKey(deleteId[i]);
				//删除流程
				activitiUtil.deleteTask(ActivitiKey.PUBLIC_KEY, fnumber[i]);
			}
		}else{
			//先删除子类信息
			carMaintainRegEntryMapper.deleteByFparentId(fid);
			//在删除父类信息
			carMaintainRegMapper.deleteByPrimaryKey(fid);
			//删除流程
			activitiUtil.deleteTask(ActivitiKey.PUBLIC_KEY, fnumber2);
		}
	}

	/**
	 * 审核，反审核
	 */
	@Override
    public int auditCarMaintainReg(String fid, String type, String fhandlerId, String message, String state, String fnumber) {
        // TODO Auto-generated method stub
		CarMaintainReg carMaintainReg = new CarMaintainReg();
        Axamine axamine = new Axamine();
        if ("1".equals(type)) {
            ServerResponse serverResponse = axamine.auditProcess(fnumber, state, message);
            if (serverResponse.getStatus() == 1) {
                //查询子类信息
                List<CarMaintainRegEntry> carMaintainRegEntrys = queryCarMaintainRegEntry(fid, "", "");
                for (CarMaintainRegEntry carMaintainRegEntry : carMaintainRegEntrys) {
                    //办理时间
                    String blsj = carMaintainRegEntry.getCfbizdate();
                    //结束时间
                    String jssj = carMaintainRegEntry.getCfenddate();
                    if (carMaintainRegEntry.getCfcarid() == null || "".equals(carMaintainRegEntry.getCfcarid())) {//车id为空就不修改

                    } else {
                        if (jssj == null || blsj == null) {//时间为空也不修改

                        } else {
                            //修改车辆信息
                            Car car = new Car();
                            car.setFid(carMaintainRegEntry.getCfcarid());
                            car.setCfbyrq(blsj);
                            car.setCfbydqr(jssj);
                            carMapper.updateByPrimaryKeySelective(car);
                        }
                    }
                }
                //fid
                carMaintainReg.setFid(fid);
                //操作人id
                carMaintainReg.setFauditorid(fhandlerId);
                //修改人
                carMaintainReg.setFlastupdateuserid(fhandlerId);
                carMaintainReg.setCfbizstate(5L);
                return carMaintainRegMapper.updateByPrimaryKeySelective(carMaintainReg);
            } else if (serverResponse.getStatus() == 2) {
                if (!"1".equals(state)) {
                    carMaintainReg.setCfbizstate(3L);
                    //fid
                    carMaintainReg.setFid(fid);
                    carMaintainRegMapper.updateByPrimaryKeySelective(carMaintainReg);
                }
                //推进
                return 2;
            } else {
                //错误
                return 0;
            }
        }else{
            axamine.againstAuditProcess(fnumber, message);
            //fid
            carMaintainReg.setFid(fid);
            //操作人id
            carMaintainReg.setFauditorid(fhandlerId);
            //修改人
            carMaintainReg.setFlastupdateuserid(fhandlerId);
            carMaintainReg.setCfbizstate(3L);
            return carMaintainRegMapper.updateByPrimaryKeySelective(carMaintainReg);
        }
    }

    @Override
    public int modifyCarMaintainReg(CarMaintainReg carMaintainReg) {
        return carMaintainRegMapper.updateByPrimaryKeySelective(carMaintainReg);
    }

    @Override
    public List<CarMaintainRegEntry> selectCarMaintainRegbyInfo(String fcompanyid, String carName, String carNumber, String beginTime, String endTime) {
        Map<String, String> map = new HashMap<>();
        map.put("fcompanyid", fcompanyid);
        map.put("carName", carName);
        map.put("carNumber", carNumber);
        map.put("beginTime", beginTime);
        map.put("endTime", endTime);
        return carMaintainRegEntryMapper.selectCarMaintainRegbyInfo(map);
    }
}
